const state = {
  createCurriculumDialog: false,
  curriculumId: '',
  curriculumInfo: {}
};
const getters = {

  isShow(state) { //方法名随意,主要是来承载变化的createCurriculumDialog的值
    return state.createCurriculumDialog
  },
  getCurriculumInfo(state) {
    return state.curriculumInfo
  },
  getCurriculumId(state) {
    return state.curriculumId;
  }

};

const mutations = {
  show(state) { //自定义改变state初始值的方法，这里面的参数除了state之外还可以再传额外的参数(变量或对象);

    state.createCurriculumDialog = true;
  },
  hide(state) { //同上
    state.curriculumId = "";
    state.curriculumInfo = {};
    state.createCurriculumDialog = false;
  },
  setCurriculumId(state, data) {
    state.curriculumId = data;
  },
  setCurriculumInfo(state, data) {
    state.curriculumInfo = data;
  }
};

//判断两个数组中的某个值是否相同
function getListToList(list1, list2) {
  if (list1.length != list2.length) return false;
  for (let i = 0; i < list1.length; i++) {
    for (let y = 0; y < list2.length; y++) {
      if (list1[i].campusId == list2[y].campusId) {
        break;
      }
      if (y == list2.length - 1) {
        return false;
      }
    }
  }
  return true;
}

const actions = {
  hidecreateCurriculumPanel(context) { //自定义触发mutations里函数的方法，context与store 实例具有相同方法和属性
    context.commit('hide');
  },
  showcreateCurriculumPanel(context) { //同上注释
    context.commit('show');
  },
  getcreateCurriculumById(context, data) {
    let paras = {
      courseId: data.id,
      campusId: data.campusId //遗留问题
    };
    var courseCampusId = data.campusId;
    context.commit('setCurriculumId', paras.courseId);
    let dicdata = tool.getItem(lsKey.dict, true);
    let user = tool.getItem(lsKey.user, true); //得到当前用户信息
    let isExtend = data.isExtend;
    console.log(user);
    if (data.id == null || data.id == undefined) {

      let courseInfo = {
        isEditableOfChargeMode: true,
        isEditableOfCourseName: true,
        courseTypeWidth: "45%",
        maxCourseLength: 1,
        isShowSchool: true,
        courseTypeList: [],
        subjectList: [],
        courseTeaacher2Id: "",
        foreignTeacherType: "",
        foreignTeacherTypeList: [],
        teacherCourseList: [],
        courseModeList: dicdata["course_mode"],
        classtext: 1,
        classList: [],
        chargeModeList: dicdata["charge_mode"],
        courseTimeModeList: dicdata["course_time_mode"],
        orgList: [],
        courseName: "",
        courseType: "",
        subjectId: "",
        courseMode: 1,
        chargeMode: 1,
        offsetDay: "",
        allowLateMinute: "",
        createOrgId: user.orgId,
        remark: "",
        coursePackageEntityList: [{
          chargeMode: 1,
          courseTimeMode: 1,
          courseHour: "",
          syllabusNum: "",
          cycle: 10,
          totalMoney: "",
          isErasable: true
        }],
        courseCampusEntityList: [],
        isShowSchoolView: false,
        schoolText: ["所有校区"],
        selectSchoolList: [],
        consumeMode: 1,
        courseItemEntityList: [{
          courseItemName: "",
          oneCourseHourMinute: "",
          use: false
        }]
      }
     
      api.getSubjectList({
          orgId: user.orgId + ""
        })

        .then(function(data) {
          //枚举科目列表
          if (data.result == "操作成功") {
            courseInfo.subjectList = data.list;
          }
          return api.getTeacherCourseList();
        })
        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {

            courseInfo.teacherCourseList = data.list;
          }
          return api.getTeacherCourseSetList();
        })
        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {

            courseInfo.foreignTeacherTypeList = data.list;
          }
          return api.getcourseTypeList();
        })
        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {

            courseInfo.courseTypeList = data.list;
          }
          return api.orgList();
        })
        .then(function(data) {
          //机构列表
          if (data.result == "操作成功") {

            courseInfo.orgList = data.list;
          }
          return api.campusList({
            orgId: user.orgId + ""
          });
        })

        .then(function(data) {
          //枚举校区列表
          if (data.result == "操作成功") {
            courseInfo.classList = data.list;
            
            if (user.roleMode == 0) {
              courseInfo.createOrgId = "";
              courseInfo.courseCampusEntityList = data.list;
            } else if (user.roleMode == 1) {
              courseInfo.createOrgId = user.orgId;
              courseInfo.courseCampusEntityList = data.list;
            } else {
              courseInfo.createOrgId = user.orgId;
              courseInfo.isShowSchool = false;
              let org = data.list.find((value) => {
                return value.campusId == courseCampusId;
              });
              courseInfo.schoolText = [org.name];
              courseInfo.courseCampusEntityList = [org];
            }
          }
          context.commit('setCurriculumInfo', courseInfo);
          context.commit('show');
        });
    } else {
      let courselisttemp = {};
      api
        .courseInfo({
          courseId: data.id
        })
        .then(function(data) {
          console.log(data);
          if (data.result == "操作成功") {
            courselisttemp = data.info;

            courselisttemp.isShowSchool = user.roleMode <= 1;
          }

          return api.getSubjectList({
            orgId: user.orgId + ""
          });
        })
        .then(function(data) {
          //枚举科目列表
          if (data.result == "操作成功") {
            courselisttemp.subjectList = data.list;
          }
          return api.getTeacherCourseList();
        })


        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {

            courselisttemp.teacherCourseList = data.list;
          }
          return api.getTeacherCourseSetList();
        })
        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {

            courselisttemp.foreignTeacherTypeList = data.list;
          }
          return api.getcourseTypeList();
        })
        .then(function(data) {
          //课程类型列表
          if (data.resultCode == 2000) {
            if (user.roleMode > 1) {
              data.list.push({
                dicValue: 4,
                name: "双师"
              });
            }
            courselisttemp.courseTypeList = data.list;
          }
          return api.orgList();
        })
        .then(function(data) {
          //机构列表
          if (data.result == "操作成功") {
            courselisttemp.orgList = data.list;
          }
          return api.campusList({
            orgId: user.orgId + ""
          });
        })
        .then(function(data) {
          if (data.result == "操作成功") {
            courselisttemp.classList = data.list;
          }
          courselisttemp.courseModeList = dicdata["course_mode"];
          courselisttemp.chargeModeList = dicdata["charge_mode"];
          // courselisttemp.foreignTeacherTypeList = dicdata["foreign_teacher_type"];
          courselisttemp.courseTimeModeList = dicdata["course_time_mode"];
          courselisttemp.selectSchoolList = [];
          courselisttemp.schoolText = [];
          courselisttemp.courseItemEntityList = courselisttemp.courseItemEntityList.length == 0 ? [{
            courseItemName: "",
            oneCourseHourMinute: "",
            use: false
          }] : courselisttemp.courseItemEntityList;
          courselisttemp.courseTypeWidth = courselisttemp.courseType == 4 ? "10%" : "45%";
          courselisttemp.maxCourseLength = 1;
          if (courselisttemp.courseType == 4) {
            let currentCourseTeaacher = courselisttemp.teacherCourseList.find(
              function(item) {
                return (
                  item.courseTeaacher2Id == courselisttemp.courseTeaacher2Id
                );
              }
            );
            courselisttemp.maxCourseLength =
              currentCourseTeaacher.syllabusCount;
          }

          if (getListToList(courselisttemp.courseCampusEntityList, courselisttemp.classList)) {
            courselisttemp.isShowSchoolView = false;
            courselisttemp.selectSchoolList = [];
            courselisttemp.schoolText = courselisttemp.courseCampusEntityList.length == 1 ? [courselisttemp.courseCampusEntityList[
              0].campusName] : ["所有校区"];
            courselisttemp.classtext = 1;
          } else {
            courselisttemp.isShowSchoolView = true;
            courselisttemp.schoolText = [];
            if (courselisttemp.courseCampusEntityList == null) {} else {
              for (let i = 0; i < courselisttemp.courseCampusEntityList.length; i++) {
                courselisttemp.selectSchoolList.push(courselisttemp.courseCampusEntityList[i].campusId);
                for (let y = 0; y < courselisttemp.classList.length; y++) {
                  if (courselisttemp.courseCampusEntityList[i].campusId == courselisttemp.classList[y].campusId) {
                    courselisttemp.schoolText.push(courselisttemp.classList[y].name);
                  }
                }
              }
            }
            courselisttemp.classtext = 2;
          }
          courselisttemp.isExtend = false;
          console.log(isExtend);
          if (isExtend) {
            courselisttemp.isExtend = true;
            courselisttemp.isEditableOfChargeMode = false;
            courselisttemp.isEditableOfCourseName = false;
          }
          context.commit('setCurriculumInfo', courselisttemp);
          context.commit('show');
        });
    }


  }
};

module.exports = {
  namespaced: true, //用于在全局引用此文件里的方法时标识这一个的文件名
  state,
  getters,
  actions,
  mutations
}
